package ro.dandanciu.webgallery.ui.users;

import org.apache.log4j.Logger;

import ro.dandanciu.webgallery.domainmodel.User;
import ro.dandanciu.webgallery.services.RegistrationRequestServices;
import ro.dandanciu.webgallery.ui.exceptions.UnknownSubmitModeException;
import ro.dandanciu.webgallery.ui.general.WebGalleryAction;

/**
 * Action for dealing with registration requests.
 *
 * @author Dan Danciu
 *
 */
public class RegistrationRequestsAction extends WebGalleryAction {
    /**
     * Logger for this class
     */
    private static final Logger logger = Logger.getLogger(RegistrationRequestsAction.class);

    private static final String MAKE_MODE = "M";

    private static final String CONF_MODE = "C";

    /**
     * The mode in which the form was submitted.
     */
    private String mode = null;

    /**
     * The userId for the registration request.
     */
    private String userId = null;

    /**
     * The email address to which to send the registration request email.
     */
    private String email = null;

    /**
     * The confirmation code.
     */
    private String code = null;

    /**
     * Business class for registration requests.
     */
    private RegistrationRequestServices registrationRequestServices;

    /**
     * Execute method for this action.
     */
    @Override
    public String execute() throws Exception {
        if (logger.isDebugEnabled()) {
            logger.debug("execute() - start");
        }
        final String toReturn;

        if (MAKE_MODE.equals(mode)) {
            registrationRequestServices.createRegistrationRequest(userId, email);
            toReturn = setMessage("registration.email.sent");
        } else if (CONF_MODE.equals(mode)) {
            final User user = registrationRequestServices.confirmRegistrationRequest(code);
            setSessionUser(user);
            toReturn = setMessage("confirmation.registration.successful");
        } else {
            throw new UnknownSubmitModeException(mode, new String[] {MAKE_MODE, CONF_MODE});
        }

        if (logger.isDebugEnabled()) {
            logger.debug("return " + toReturn);
            logger.debug("execute() - end");
        }

        return toReturn;

    }

    /**
     * Setter for userId.
     *
     * @param userId the user id to set.
     */
    public final void setUserId(final String userId) {
        this.userId = userId;
    }

    /**
     * Setter for email.
     *
     * @param email the email address to set.
     */
    public final void setEmail(final String email) {
        this.email = email;
    }

    /**
     * Setter for the mode of submit.
     * @param mode the mode to set
     */
    public final void setMode(final String mode) {
        this.mode = mode;
    }


    /**
     * Setter for the code.
     * @param code the code to set.
     */
    public final void setCode(final String code) {
        this.code = code;
    }

    /**
     * Setter for the registrationRequestServices.
     * @param registrationRequestServices the registrationRequestServices to set.
     */
    public final void setRegistrationRequestServices(
            final RegistrationRequestServices registrationRequestServices) {
        this.registrationRequestServices = registrationRequestServices;
    }
}
